package com.example.backend.service;

import com.example.backend.entity.User;
import com.example.backend.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
@Transactional
public class UserService {
    @Autowired
    private UserMapper userMapper;
    public int addUser(User user)
    {
        return userMapper.addUser(user);
    }
    public int updateUser(User user)
    {
        return userMapper.updateUser(user);
    }
    public int deleteUserById(String id)
    {
        return userMapper.deleteUserById(id);
    }
    public User selectUserById(String id)
    {
        return userMapper.selectUserById(id);
    }
    public User selectUser(User user)
    {
        return userMapper.selectUser(user);
    }
    public Map<String, Object> selectAll(User user, int pageNum, int pageSize)
    {
        PageHelper.startPage(pageNum, pageSize);
        ArrayList<User> users = userMapper.selectUsers(user);

        PageInfo<User> pageInfo = new PageInfo<>(users);

        Map<String, Object> result = new HashMap<>();
        result.put("list", users);
        result.put("total", pageInfo.getTotal());
        result.put("pages", pageInfo.getPages());

        return result;
    }
    public ArrayList<User> selectUsers(User user) {
        return userMapper.selectUsers(user);
    }
    public boolean register(User user) {
        return userMapper.register(user).size()>0;
    }
}
